/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapeClient;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marco
 */
public class UDPServiceProxy implements ServiceProxy{
    
    private Inet4Address IpAddress;
    private int port;
    
    
    public UDPServiceProxy(String IpAddress, int port){
        this.setIpAddress(IpAddress);
        this.setPort(port);
    }
    
    
    @Override
    public void enqueue(String event) {
        try {
            
            DatagramSocket socket = new DatagramSocket();
            byte[] message = event.getBytes();
            
            DatagramPacket packet = new DatagramPacket(message, message.length, IpAddress, port);
            
            socket.send(packet);
            
        } catch (IOException ex) {
            Logger.getLogger(UDPServiceProxy.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    

    public Inet4Address getIpAddress() {
        return IpAddress;
    }

    public void setIpAddress(String IpAddress) {
        try {
            this.IpAddress = (Inet4Address) Inet4Address.getByName(IpAddress);
        } catch (UnknownHostException ex) {
            Logger.getLogger(UDPServiceProxy.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

   
    
    
    
}
